import request from "../utils/request"

import { Storage } from "../store/storage";

import {Message} from "element-ui"

/**
 * 
 * @param {object} data 
 * @param {VueComponent} component 
 * @returns 
 */
function getList(data, component) {
  return request.post("/withdrawal/list", data).then(res => {
    component.list = res.data.data;

    component.total = res.data.total;

    Storage.tranferByStorage(res.data.data);

    console.log(res)
    return res;
  });
}
/**
 * 
 * @param {object} data 
 * @param {VueComponent} component 
 * @returns 
 */
function getListBySearch(data, component) {
  return request.post("/withdrawal/list", data).then(res => {
    component.list = res.data.data;
    component.total = res.data.total;
    return res;
  });
}

/**
 * 
 * @param {object} data 
 * @param {VueComponent} component 
 * @returns 
 */
function withdrawalAgin(data, component, index) {
  return request.post("/withdrawal/withdrawalAgin", data).then(res => {

    // 轮询提现转移
    let count = 0;
    let id = setInterval(() => {
      count++;
      getWithdrawalById(data, component, index);
      console.log("xount", count);
      if (6 <= count) {
        clearInterval(id);

        Message({
          message: "轮询查询提现状态结束",
          type: 'error',
          duration: 3 * 1000
        });
    
      }
    }, 3000);
    component.list = res.data;
    
    return res;
  });
}


/**
 * 
 * @param {object} data 
 * @param {VueComponent} component 
 * @returns 
 */
function getWithdrawalById(data, component, index) {
  return request.post("/withdrawal/getWithdrawalById", data).then(res => {
    let msg = [
      "失败",
      "成功"
    ];

    let type = [
      'error',
      'success'
    ];
    Message({
      message: "轮询查询提现状态：" + msg[res.data.alreadyCasting],
      type: type[res.data.alreadyCasting],
      duration: 3 * 1000
    });

    if (res.data.alreadyCasting) {

      let item = component.list[index];

      item.alreadyCasting = res.data.alreadyCasting;

      component.$set(component.list, index, item);
    }

    return res;
  });
}


export {
  getList,
  withdrawalAgin,
  getWithdrawalById,
  getListBySearch
}